package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImp implements DishService {

    @Resource
    DishMapper dishMapper;
    @Resource
    DishFlavorMapper dishFlavorMapper;

    @Resource
    SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult page(DishPageQueryDTO page) {
        PageHelper.startPage(page.getPage(), page.getPageSize());
        Page<DishVO> db = dishMapper.pageQuery(page);

        return new PageResult(db.getTotal(), db.getResult());
    }
    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Dish byId = dishMapper.getById(id);
            if (byId.getStatus() == 1) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        List<SetmealDish> setmealDishByIds = setmealDishMapper.getSetmealDishByIds(ids);
        if (setmealDishByIds != null && !setmealDishByIds.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        dishMapper.delete(ids);
        dishFlavorMapper.deleteByDishIds(ids);

    }

    @Override
    public DishVO getDishWithFlavor(Long id) {
        Dish byId = dishMapper.getById(id);
        List<DishFlavor> list = dishFlavorMapper.getByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(byId,dishVO);
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Override
    public void updateDishWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        Long dishid = dish.getId();



        dishMapper.update(dish);

        List<Long> dishIds = new ArrayList<>();
        dishIds.add(dish.getId());

        //修改dishflavor 修改口味 先删除在插入

        if(flavors!=null && !flavors.isEmpty()){
            for (DishFlavor dishFlavor :flavors){
                dishFlavor.setDishId(dishid);
            };
            dishFlavorMapper.deleteByDishIds(dishIds);
            dishFlavorMapper.insertBatch(flavors);
        }

    }
   /*
   * 通过分类id查询dish
   * */
    @Override
    public List<Dish> getDishByCategoryId(Long id) {
        return dishMapper.getByCategoryId(id);
    }

    /*
     * 保存菜品数据和口味数据
     * */
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.insert(dish);
        Long id = dish.getId();

        List<DishFlavor> flavors = dto.getFlavors();

        if (flavors != null && !flavors.isEmpty()) {
            for (DishFlavor it : flavors) {
                it.setDishId(id);
            }
            dishFlavorMapper.insertBatch(flavors);
        }


    }
}
